﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.Maps.MapControl;

namespace imaginecup
{
    public partial class SearchPlace : UserControl
    {
        private ServiceReference1.DataProviderSoapClient getPosition = new imaginecup.ServiceReference1.DataProviderSoapClient();
        List<ServiceReference1.CPosition> plist;

        public SearchPlace()
        {
            InitializeComponent();
            getPosition.getPositionsCompleted += new EventHandler<imaginecup.ServiceReference1.getPositionsCompletedEventArgs>(getPosition_getPositionsCompleted);
            btnSearch.Click += new RoutedEventHandler(btnSearch_Click);
            lbPlace.SelectionChanged += new SelectionChangedEventHandler(lbPlace_SelectionChanged);
            
        }

        void lbPlace_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string str = e.AddedItems[0].ToString();
            int i;
            for (i = 0; i < plist.Count; i++)
            {
                if (plist[i].Name == str)
                    break;
            }

            if (i == plist.Count)
            {
                throw new Exception("error");
            }
            else
            {
                //选择相应区域
                Public.p.pmap.testmap.SetView(new Location(plist[i].Latitude, plist[i].Longtitude), plist[i].ZoomLevel);
                ((SearchPlaceWin)(this.Parent)).Close();
            }
        }

        void getPosition_getPositionsCompleted(object sender, imaginecup.ServiceReference1.getPositionsCompletedEventArgs e)
        {
            plist = new List<imaginecup.ServiceReference1.CPosition>(e.Result);

            for (int i = 0; i < plist.Count; i++)
            {
                lbPlace.Items.Add(plist[i].Name);
            }
        }

        void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            getPosition.getPositionsAsync(tbKey.Text);
        }
    }
}
